import {
  createRouter,
  createWebHashHistory,
} from "vue-router";
import HomeView from "../views/home/index.vue";

export const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/kind",
    name: "kind",
    component: () => import("../views/kind/index.vue"),
  },
  {
    path: "/cart",
    name: "cart",
    component: () => import("../views/cart/index.vue"),
  },
  {
    path: "/order",
    name: "order",
    meta: {
      hidden: true
    },
    component: () => import("../views/order/index.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/my/index.vue"),
  },
  {
    path: "/login",
    name: "login",
    // 添加元信息
    meta: {
      hidden: true
    },
    component: () => import("../views/login/index.vue"),
  },
  {
    path: '/detail',
    name: 'detial',
    meta: {
      hidden: true
    },
    component: () => import('../views/detial/index.vue')
  },
  {
    path: "/register",
    name: "register",
    meta: {
      hidden: true
    },
    component: () => import("../views/register/index.vue"),
    children: [
      {
        path: 'step1',
        name: 'step1',
        component: () => import("../views/register/step1.vue"),
      },
      {
        path: 'step2',
        name: 'step2',
        component: () => import("../views/register/step2.vue"),
      },
      {
        path: 'step3',
        name: 'step3',
        component: () => import("../views/register/step3.vue"),
      }
    ]
  },
  {
    path: "/money",
    name: "money",
    // 添加元信息
    meta: { hidden: true },
    component: () => import("../views/money/index.vue"),
  },
  {
    path: "/personal",
    name: "personal",
    // 添加元信息
    meta: { hidden: true },
    component: () => import("../views/personal/index.vue"),
  },

];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});

// router.beforeEach((to, from) => {
//   if (to.fullPath === '/cart' || to.fullPath === '/my') {
//     console.log(to);
//   }
//   // 返回 false 以取消导航
//   // return false
// })
router.beforeEach(
  (to, from) => {
    // const userStore = useUserStore()
    if (to.fullPath === '/cart' || to.fullPath === '/my') {
      // console.log(window.localStorage.getItem('userstate'));
      if(!window.localStorage.getItem('userstate')){
        router.push('/login')
      }
    }
    // return false
  }
)
export default router;
